package com.hilaver.dzmis.service.impl;

import java.util.List;

import com.hilaver.dzmis.Constants;
import com.hilaver.dzmis.basicinfo.BiSwatch;
import com.hilaver.dzmis.codetable.CtFilsType;
import com.hilaver.dzmis.codetable.CtSwatchType;
import com.hilaver.dzmis.codetable.CtYarnMaterial;
import com.hilaver.dzmis.dao.BiSwatchDAO;
import com.hilaver.dzmis.service.AbstractBaseService;
import com.hilaver.dzmis.service.IEntityService;
import com.hilaver.dzmis.util.SimpleObj2XML;
import com.hilaver.dzmis.util.StringUtils;

public class BiSwatchServiceImpl extends AbstractBaseService implements
		IEntityService {
	
	private CtServiceImpl ctService;
	
	private BiSwatchDAO dao;
	
	public BiSwatchServiceImpl(){
		this.dao = new BiSwatchDAO();
		this.ctService = new CtServiceImpl();
	}
	
	@Override
	public String delete(int id) throws Exception {
		super.editSysMbox(BiSwatch.class.getName(), id);
		return super.delete(BiSwatch.class.getName(), id);
	}

	public String edit(BiSwatch biSwatch) throws Exception {
		String operation = null;
		if(biSwatch.getId() != null){
			BiSwatch old = (BiSwatch)this.dao.get(BiSwatch.class.getName(), biSwatch.getId());
			if(biSwatch.getPhoto1Name() == null){
				biSwatch.setPhoto1(old.getPhoto1());
				biSwatch.setPhoto1Name(old.getPhoto1Name());
			}
			if(biSwatch.getPhoto2Name() == null){
				biSwatch.setPhoto2(old.getPhoto2());
				biSwatch.setPhoto2Name(old.getPhoto2Name());
			}
			if(biSwatch.getPhoto3Name() == null){
				biSwatch.setPhoto3(old.getPhoto3());
				biSwatch.setPhoto3Name(old.getPhoto3Name());
			}
			if(biSwatch.getPhoto4Name() == null){
				biSwatch.setPhoto4(old.getPhoto4());
				biSwatch.setPhoto4Name(old.getPhoto4Name());
			}
			this.dao.getSession().evict(old);
			operation = "02";
		}else{
			operation = "01";
		}
		this.dao.saveOrUpdate(biSwatch);
		super.editSysMbox(biSwatch, operation);
		return "{success: true}";
	}
	
	@Override
	public String get(int id) throws Exception {
		BiSwatch biSwatch = (BiSwatch)this.dao.get(BiSwatch.class.getName(), id);
		
		writeFile(realFileDir + biSwatch.getPhoto1Name(), biSwatch.getPhoto1());
		writeFile(realFileDir + biSwatch.getPhoto2Name(), biSwatch.getPhoto2());
		writeFile(realFileDir + biSwatch.getPhoto3Name(), biSwatch.getPhoto3());
		writeFile(realFileDir + biSwatch.getPhoto4Name(), biSwatch.getPhoto4());
		
		StringBuffer xmlSB = new StringBuffer();
		xmlSB.append(Constants.XML_DOC_DEFINE);
		xmlSB.append(Constants.XML_ROOT_TAG[0]);
		xmlSB.append(Constants.XML_ITEM_TAG[0]);
		
		if(biSwatch.getBiFactory() != null){
			xmlSB.append("<" + COLUMN_FACTORY_ID + ">");
			xmlSB.append(Constants.XML_CDATA[0]);
			xmlSB.append(biSwatch.getBiFactory().getId());
			xmlSB.append(Constants.XML_CDATA[1]);
			xmlSB.append("</" + COLUMN_FACTORY_ID + ">");
			
			xmlSB.append("<factoryNameCN>");
			xmlSB.append(Constants.XML_CDATA[0]);
			xmlSB.append(biSwatch.getBiFactory().getNameCN());
			xmlSB.append(Constants.XML_CDATA[1]);
			xmlSB.append("</factoryNameCN>");
			
			xmlSB.append("<factoryNameEN>");
			xmlSB.append(Constants.XML_CDATA[0]);
			xmlSB.append(biSwatch.getBiFactory().getNameEN());
			xmlSB.append(Constants.XML_CDATA[1]);
			xmlSB.append("</factoryNameEN>");
			
			xmlSB.append("<addressFactoryCN>");
			xmlSB.append(Constants.XML_CDATA[0]);
			xmlSB.append(biSwatch.getBiFactory().getAddressFactoryCN());
			xmlSB.append(Constants.XML_CDATA[1]);
			xmlSB.append("</addressFactoryCN>");
			
			xmlSB.append("<addressFactoryEN>");
			xmlSB.append(Constants.XML_CDATA[0]);
			xmlSB.append(biSwatch.getBiFactory().getAddressFactoryEN());
			xmlSB.append(Constants.XML_CDATA[1]);
			xmlSB.append("</addressFactoryEN>");
		}
		
		if(biSwatch.getBiArmor().getId() != null){
			xmlSB.append("<armorId>");
			xmlSB.append(Constants.XML_CDATA[0]);
			xmlSB.append(biSwatch.getBiArmor().getId());
			xmlSB.append(Constants.XML_CDATA[1]);
			xmlSB.append("</armorId>");
			
			xmlSB.append("<armorReference>");
			xmlSB.append(Constants.XML_CDATA[0]);
			xmlSB.append(biSwatch.getBiArmor().getReference());
			xmlSB.append(Constants.XML_CDATA[1]);
			xmlSB.append("</armorReference>");
		}
		
		xmlSB.append("<photo1Name>");
		xmlSB.append(Constants.XML_CDATA[0]);
		if(biSwatch.getPhoto1Name() == null){
			xmlSB.append(NO_IMAGE_NAME);
		}else{
			xmlSB.append(biSwatch.getPhoto1Name());
		}
		xmlSB.append(Constants.XML_CDATA[1]);
		xmlSB.append("</photo1Name>");
		
		xmlSB.append("<photo2Name>");
		xmlSB.append(Constants.XML_CDATA[0]);
		if(biSwatch.getPhoto2Name() == null){
			xmlSB.append(NO_IMAGE_NAME);
		}else{
			xmlSB.append(biSwatch.getPhoto2Name());
		}
		xmlSB.append(Constants.XML_CDATA[1]);
		xmlSB.append("</photo2Name>");
		
		xmlSB.append("<photo3Name>");
		xmlSB.append(Constants.XML_CDATA[0]);
		if(biSwatch.getPhoto3Name() == null){
			xmlSB.append(NO_IMAGE_NAME);
		}else{
			xmlSB.append(biSwatch.getPhoto3Name());
		}
		xmlSB.append(Constants.XML_CDATA[1]);
		xmlSB.append("</photo3Name>");
		
		xmlSB.append("<photo4Name>");
		xmlSB.append(Constants.XML_CDATA[0]);
		if(biSwatch.getPhoto4Name() == null){
			xmlSB.append(NO_IMAGE_NAME);
		}else{
			xmlSB.append(biSwatch.getPhoto4Name());
		}
		xmlSB.append(Constants.XML_CDATA[1]);
		xmlSB.append("</photo4Name>");
		
		xmlSB.append(SimpleObj2XML.toXMLFragment(biSwatch));
		
		xmlSB.append(Constants.XML_ITEM_TAG[1]);
		xmlSB.append(Constants.XML_ROOT_TAG[1]);
		
		return xmlSB.toString();
	}

	@Override
	public String getAllPagination(int page, String sort, String order)
			throws Exception {
		// TODO Auto-generated method stub
		return null;
	}
	
	public String getAllPagination(int page, String sort, String order,
			String[] filters) throws Exception {
		int offset = Constants.PAGE_SIZE * (page - 1);
		List<BiSwatch> objList = this.dao.getAllPagination(BiSwatch.class
				.getName(), offset, Constants.PAGE_SIZE, sort, order, filters);
		
		StringBuffer xmlSB = new StringBuffer();
		xmlSB.append(Constants.XML_DOC_DEFINE);
		xmlSB.append(Constants.XML_ROOT_TAG[0]);
		for (BiSwatch obj : objList) {
			writeFile(realFileDir + obj.getPhoto1Name(), obj.getPhoto1());
			writeFile(realFileDir + obj.getPhoto2Name(), obj.getPhoto2());
			writeFile(realFileDir + obj.getPhoto3Name(), obj.getPhoto3());
			writeFile(realFileDir + obj.getPhoto4Name(), obj.getPhoto4());
			
			xmlSB.append(Constants.XML_ITEM_TAG[0]);
			xmlSB.append(Constants.XML_INDEX_TAG[0]);
			xmlSB.append(++offset);
			xmlSB.append(Constants.XML_INDEX_TAG[1]);

			xmlSB.append("<ctSwatchType>");
			xmlSB.append(Constants.XML_CDATA[0]);
			xmlSB.append(this.ctService.getDescription(CtSwatchType.class.getName(), obj.getCtSwatchType(), locale));			
			xmlSB.append(Constants.XML_CDATA[1]);
			xmlSB.append("</ctSwatchType>");
			
			xmlSB.append("<ctFilsType>");
			xmlSB.append(Constants.XML_CDATA[0]);
			xmlSB.append(this.ctService.getDescription(CtFilsType.class.getName(), obj.getCtFilsType(), locale));			
			xmlSB.append(Constants.XML_CDATA[1]);
			xmlSB.append("</ctFilsType>");
			
			xmlSB.append("<ctYarnMaterial1>");
			xmlSB.append(Constants.XML_CDATA[0]);
			xmlSB.append(this.ctService.getDescription(CtYarnMaterial.class.getName(), obj.getCtYarnMaterial1(), locale));
			if(!StringUtils.isEmpty(obj.getCtYarnMaterial1()) && obj.getComposition1() != null){
				xmlSB.append("&nbsp;(&nbsp;" + obj.getComposition1() + "%&nbsp;)");
			}
			xmlSB.append(Constants.XML_CDATA[1]);
			xmlSB.append("</ctYarnMaterial1>");
			
			xmlSB.append("<ctYarnMaterial2>");
			xmlSB.append(Constants.XML_CDATA[0]);
			xmlSB.append(this.ctService.getDescription(CtYarnMaterial.class.getName(), obj.getCtYarnMaterial2(), locale));
			if(!StringUtils.isEmpty(obj.getCtYarnMaterial2()) && obj.getComposition2() != null){
				xmlSB.append("&nbsp;(&nbsp;" + obj.getComposition2() + "%&nbsp;)");
			}
			xmlSB.append(Constants.XML_CDATA[1]);
			xmlSB.append("</ctYarnMaterial2>");
			
			xmlSB.append("<ctYarnMaterial3>");
			xmlSB.append(Constants.XML_CDATA[0]);
			xmlSB.append(this.ctService.getDescription(CtYarnMaterial.class.getName(), obj.getCtYarnMaterial3(), locale));
			if(!StringUtils.isEmpty(obj.getCtYarnMaterial3()) && obj.getComposition3() != null){
				xmlSB.append("&nbsp;(&nbsp;" + obj.getComposition3() + "%&nbsp;)");
			}
			xmlSB.append(Constants.XML_CDATA[1]);
			xmlSB.append("</ctYarnMaterial3>");
			
			xmlSB.append("<ctYarnMaterial4>");
			xmlSB.append(Constants.XML_CDATA[0]);
			xmlSB.append(this.ctService.getDescription(CtYarnMaterial.class.getName(), obj.getCtYarnMaterial4(), locale));
			if(!StringUtils.isEmpty(obj.getCtYarnMaterial4()) && obj.getComposition4() != null){
				xmlSB.append("&nbsp;(&nbsp;" + obj.getComposition4() + "%&nbsp;)");
			}
			xmlSB.append(Constants.XML_CDATA[1]);
			xmlSB.append("</ctYarnMaterial4>");
			
			xmlSB.append("<ctYarnMaterial5>");
			xmlSB.append(Constants.XML_CDATA[0]);
			xmlSB.append(this.ctService.getDescription(CtYarnMaterial.class.getName(), obj.getCtYarnMaterial5(), locale));
			if(!StringUtils.isEmpty(obj.getCtYarnMaterial5()) && obj.getComposition5() != null){
				xmlSB.append("&nbsp;(&nbsp;" + obj.getComposition5() + "%&nbsp;)");
			}
			xmlSB.append(Constants.XML_CDATA[1]);
			xmlSB.append("</ctYarnMaterial5>");
			
			
			if(obj.getBiFactory() != null){
				xmlSB.append("<factoryNameCN>");
				xmlSB.append(Constants.XML_CDATA[0]);
				xmlSB.append(obj.getBiFactory().getNameCN());
				xmlSB.append(Constants.XML_CDATA[1]);
				xmlSB.append("</factoryNameCN>");
				
				xmlSB.append("<factoryNameEN>");
				xmlSB.append(Constants.XML_CDATA[0]);
				xmlSB.append(obj.getBiFactory().getNameEN());
				xmlSB.append(Constants.XML_CDATA[1]);
				xmlSB.append("</factoryNameEN>");
				
				xmlSB.append("<addressFactoryCN>");
				xmlSB.append(Constants.XML_CDATA[0]);
				xmlSB.append(obj.getBiFactory().getAddressFactoryCN());
				xmlSB.append(Constants.XML_CDATA[1]);
				xmlSB.append("</addressFactoryCN>");
				
				xmlSB.append("<addressFactoryEN>");
				xmlSB.append(Constants.XML_CDATA[0]);
				xmlSB.append(obj.getBiFactory().getAddressFactoryEN());
				xmlSB.append(Constants.XML_CDATA[1]);
				xmlSB.append("</addressFactoryEN>");
			}
			
			if(obj.getBiArmor() != null){
				xmlSB.append("<armorReference>");
				xmlSB.append(Constants.XML_CDATA[0]);
				xmlSB.append(obj.getBiArmor().getReference());
				xmlSB.append(Constants.XML_CDATA[1]);
				xmlSB.append("</armorReference>");
			}
			
			xmlSB.append("<photo1Name>");
			xmlSB.append(Constants.XML_CDATA[0]);
			if(obj.getPhoto1Name() == null){
				xmlSB.append(NO_IMAGE_NAME);
			}else{
				xmlSB.append(obj.getPhoto1Name());
			}
			xmlSB.append(Constants.XML_CDATA[1]);
			xmlSB.append("</photo1Name>");
			
			xmlSB.append("<photo2Name>");
			xmlSB.append(Constants.XML_CDATA[0]);
			if(obj.getPhoto2Name() == null){
				xmlSB.append(NO_IMAGE_NAME);
			}else{
				xmlSB.append(obj.getPhoto2Name());
			}
			xmlSB.append(Constants.XML_CDATA[1]);
			xmlSB.append("</photo2Name>");
			
			xmlSB.append("<photo3Name>");
			xmlSB.append(Constants.XML_CDATA[0]);
			if(obj.getPhoto3Name() == null){
				xmlSB.append(NO_IMAGE_NAME);
			}else{
				xmlSB.append(obj.getPhoto3Name());
			}
			xmlSB.append(Constants.XML_CDATA[1]);
			xmlSB.append("</photo3Name>");
			
			xmlSB.append("<photo4Name>");
			xmlSB.append(Constants.XML_CDATA[0]);
			if(obj.getPhoto4Name() == null){
				xmlSB.append(NO_IMAGE_NAME);
			}else{
				xmlSB.append(obj.getPhoto4Name());
			}
			xmlSB.append(Constants.XML_CDATA[1]);
			xmlSB.append("</photo4Name>");
			
			xmlSB.append(SimpleObj2XML.toXMLFragment(obj));

			xmlSB.append(Constants.XML_ITEM_TAG[1]);
		}

		xmlSB.append(Constants.XML_CURRENT_PAGE_TAG[0]);
		xmlSB.append(page);
		xmlSB.append(Constants.XML_CURRENT_PAGE_TAG[1]);

		xmlSB.append(Constants.XML_ITEM_TOTAL_TAG[0]);
		xmlSB.append(this.dao.getCount(BiSwatch.class.getName(),
				filters));
		xmlSB.append(Constants.XML_ITEM_TOTAL_TAG[1]);
		xmlSB.append(Constants.XML_ROOT_TAG[1]);
		return xmlSB.toString();
	}
	
	public String getMaxReferenceIndex(String ctSwatchType) throws Exception{
		List<Integer> referenceIndexList = this.dao.getMaxReferenceIndex(ctSwatchType);
		StringBuffer xmlSB = new StringBuffer();
		xmlSB.append(Constants.XML_DOC_DEFINE);
		xmlSB.append(Constants.XML_ROOT_TAG[0]);
		xmlSB.append(Constants.XML_ITEM_TAG[0]);
		xmlSB.append(Constants.XML_CDATA[0]);
		if(referenceIndexList.size() == 0 || referenceIndexList.get(0) == null){
			xmlSB.append("0001");
		}else{
			Integer index = referenceIndexList.get(0);
			xmlSB.append(StringUtils.to4Digital(++index));
		}
		xmlSB.append(Constants.XML_CDATA[1]);
		xmlSB.append(Constants.XML_ITEM_TAG[1]);
		xmlSB.append(Constants.XML_ROOT_TAG[1]);
		return xmlSB.toString();
	}

}
